//
//  AboutViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-11.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "AboutViewController.h"
#import "SimplePingHelper.h"


@interface AboutViewController ()
@property (strong, nonatomic) UITextView *textView;
@property (strong, nonatomic) NSString *trackViewUrl;
@end

@implementation AboutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"about", @"关于 About");
    }
    return self;
}

- (void)viewDidLoad
{
    //
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ChatBackground_00.jpg"]];
    [imageView setFrame:[[UIScreen mainScreen] bounds]];
    [self.view insertSubview:imageView atIndex:0];
    
    self.nameForSortLabel.text = NSLocalizedString(@"Video DoorBell", @" 远程可视对讲 Video DoorBell ");
    
    
    [self.scrollview setScrollEnabled:YES];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    NSLog(@"%f",r.size.height);
    [self.scrollview setContentSize:CGSizeMake(320, r.size.height - 42)];

}

- (IBAction)checkUpdateButtonPressed:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isConnectNet = [defaults boolForKey:@"ISREACHABILITY"];
    if (isConnectNet == YES)
    {
        self.waitAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"checking", @"正在检查 Checking") delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
        [self.waitAlertView show];
        [self pingAddress:@"www.baidu.com"];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"please try again connect to the network", @"请连接网络再进行尝试 Please try again connect to the network") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
        [alert show];
    }
    
    
}

- (IBAction)AdviceAndEvaluationButtonPressed:(id)sender
{
    //跳转到AppStore指定界面
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/yuan-cheng-ke-shi-dui-jiang/id848903742?mt=8&uo=4"]];
}


#pragma mark -
#pragma mark pingMethod
- (void)pingAddress:(NSString *)addr
{
    [SimplePingHelper ping:addr target:self sel:@selector(pingResult:)];
}

// callback
- (void)pingResult:(NSNumber*)success
{
    if (success.boolValue)
    {
        //查找一个可用的全局队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //将要做的操作放入队列
        dispatch_async(queue,^{
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:@"http://itunes.apple.com/lookup?id=848903742"]];
            [request setHTTPMethod:@"GET"];
            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
            
            NSString *latestVersion = [[NSString alloc] init];
            //地址trackViewUrl
            self.trackViewUrl = [[NSString alloc] init];
            //trackName
            NSString *trackName = [[NSString alloc] init];
            
            NSArray *releaseInfoArray = jsonData[@"results"];
            for (NSDictionary *releaseInfo in releaseInfoArray)
            {
               latestVersion =  [releaseInfo objectForKey:@"version"];
               self.trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];
               trackName = [releaseInfo objectForKey:@"trackName"];
            }
            
            NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
            NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
            double doubleCurrentVersion = [currentVersion doubleValue];
            double doubleUpdateVersion = [latestVersion doubleValue];
            
            dispatch_async(dispatch_get_main_queue(),^{
                [self.waitAlertView dismissWithClickedButtonIndex:0 animated:YES];
                
                if (doubleCurrentVersion < doubleUpdateVersion) {
                    
                    UIAlertView *alert;
                    alert = [[UIAlertView alloc] initWithTitle:trackName message:NSLocalizedString(@"A new version, you want to update", @"有新版本，你想要更新吗？ A new version, you want to update?") delegate: self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消 cancel") otherButtonTitles: NSLocalizedString(@"Update", @"更新 Update"), nil];
                    alert.tag = 1001;
                    [alert show];
                }
                else{
                    UIAlertView *alert;
                    alert = [[UIAlertView alloc] initWithTitle:trackName message:NSLocalizedString(@"the latest version is already", @"已经是最新版本 The latest version is already") delegate: nil cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
                    [alert show];
                }
           });
        });
    }
    else
    {
        [self.waitAlertView dismissWithClickedButtonIndex:0 animated:YES];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"under the current state of the network can not connect to the server, check whether the network can connect to the Internet", @"当前网络状态下不能连接到服务器，请检查网络是否能连接到互联网 Under the current state of the network can not connect to the server, check whether the network can connect to the Internet") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
        [alertView show];
    }
}

#pragma mark -
#pragma mark alertDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1001)
    {
        if (buttonIndex == 0)
        {
            //取消
            NSLog(@"取消");
        }
        else if (buttonIndex == 1)
        {
            //跳转到AppStore
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
        }
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
